﻿// -----------------------------------------------------------------------
// <copyright file="CustomContextMenu.cs" company="Abyssalsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace Gui.ControlBuilders
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Xml;
    using Metal.Config;
    using Metal.Resources;
    using Gui.ThemeEngine;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class CustomContextMenu : BaseControl 
    {
        /// <summary>
        /// Create the custom control
        /// </summary>
        /// <param name="controlProperties">The Xml node that contains the control properties</param>
        /// <returns>Returns custom control</returns>
        public Controls.MetalContextMenu GetControl(XmlNode controlProperties)
        {
            string strThemeName = Settings.Instance.GetValue("LastSettings.Theme", "BlackMetal");

            Controls.MetalContextMenu customMenu = new Controls.MetalContextMenu();

            // Apply common properties
            this.BuildControl(controlProperties, customMenu as Control);

            // Load ContextMenu style
            string strConfigFile = Paths.ThemesFolder + strThemeName + "\\contextmenu.xml";
            XmlDocument xmlConfig = new XmlDocument();
            xmlConfig.Load(strConfigFile);
            XmlNode menuItem = xmlConfig.SelectSingleNode("ContextMenu/MenuItem");
            XmlNode dropDownItem = xmlConfig.SelectSingleNode("ContextMenu/DropDown");

            // Set font
            FontStyle style = (FontStyle)Enum.Parse(typeof(FontStyle), PropertyProvider.GetValue(menuItem, "fontStyle", "Regular"));
            customMenu.SetFont(PropertyProvider.GetValue(menuItem, "font", "Default"), PropertyProvider.GetValue(menuItem, "fontSize", 10), style);

            foreach (XmlNode itemMenu in controlProperties.ChildNodes)
            {
                if (itemMenu.Name == "Separator")
                {
                    customMenu.Items.Add(new ToolStripSeparator());
                }
                else
                {
                    string strText = MessageProvider.GetMessage(PropertyProvider.GetValue(itemMenu, "resourceID", string.Empty));
                    Image img = Image.FromFile(Paths.ThemesFolder + strThemeName + "\\" + PropertyProvider.GetValue(itemMenu, "image", "no-image.png"));
                    ToolStripMenuItem item = new ToolStripMenuItem();
                    item.Tag = PropertyProvider.GetValue(itemMenu, "command", string.Empty);
                    item.Text = strText;

                    // Temporarly commented:
                    ////item.Image = img;

                    // Menu item
                    Controls.MetalContextMenu.menuItemSelected = PropertyProvider.GetValue(menuItem, "colorSelected");
                    Controls.MetalContextMenu.menuForeColor = PropertyProvider.GetValue(menuItem, "foreColor");
                    Controls.MetalContextMenu.menuItemSelectedGradientBegin = PropertyProvider.GetValue(menuItem, "colorGradientBegin");
                    Controls.MetalContextMenu.menuItemSelectedGradientEnd = PropertyProvider.GetValue(menuItem, "colorGradientEnd");
                    Controls.MetalContextMenu.menuItemPressedGradientBegin = PropertyProvider.GetValue(menuItem, "pressedGradientBegin");
                    Controls.MetalContextMenu.menuItemPressedGradientEnd = PropertyProvider.GetValue(menuItem, "pressedGradientEnd");
                    Controls.MetalContextMenu.menuBorder = PropertyProvider.GetValue(menuItem, "colorBorder");
                    Controls.MetalContextMenu.menuItemBorder = PropertyProvider.GetValue(menuItem, "colorBorder");

                    // Drop down
                    Controls.MetalContextMenu.dropDownMenuBackColor = PropertyProvider.GetValue(dropDownItem, "backColor");
                    Controls.MetalContextMenu.dropDownMenuForeColor = PropertyProvider.GetValue(dropDownItem, "foreColor");
                    Controls.MetalContextMenu.imageMarginGradientBegin = PropertyProvider.GetValue(dropDownItem, "imageMarginGradientBegin");
                    Controls.MetalContextMenu.imageMarginGradientMiddle = PropertyProvider.GetValue(dropDownItem, "imageMarginGradientMiddle");
                    Controls.MetalContextMenu.imageMarginGradientEnd = PropertyProvider.GetValue(dropDownItem, "imageMarginGradientEnd");

                    customMenu.Items.Add(item);
                }
            }

            return customMenu;
        }

        /// <summary>
        /// Apply common properties
        /// </summary>
        /// <param name="control">The Xml node that contains our control properties</param>
        /// <param name="customControl">The custom control that will be created</param>
        public override void BuildControl(System.Xml.XmlNode control, System.Windows.Forms.Control customControl)
        {
            base.BuildControl(control, customControl);
        }
    }
}
